package crp.core.space.domain.repositories;

import com.qwlabs.lang.C2;
import crp.core.space.domain.models.LocationEntity;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.common.Parameters;

import javax.enterprise.context.ApplicationScoped;
import java.util.Collection;
import java.util.stream.Stream;

@ApplicationScoped
public class LocationRepository implements PanacheRepositoryBase<LocationEntity, String> {
    public Stream<LocationEntity> findRoot() {
        return find("parent is null").stream();
    }

    public Stream<LocationEntity> findByParentIds(Collection<String> parentIds) {
        return C2.mapNotEmpty(parentIds, () ->
                find("parent.id in (:parentIds)", Parameters.with("parentIds", parentIds)))
                .map(PanacheQuery::stream)
                .orElse(Stream.empty());
    }

    public Stream<LocationEntity> findByIds(Collection<String> ids) {
        return C2.mapNotEmpty(ids, () ->
                find("id in (:ids)", Parameters.with("ids", ids)))
                .map(PanacheQuery::stream)
                .orElse(Stream.empty());
    }
}
